/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cursoexperto.jftf.carrocompra.entidades;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import javax.faces.context.FacesContext;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.cursoexperto.jftf.carrocompra.services.CategoriaService;

/**
 *
 * @author Jose Fernando
 */
@Entity
@Table(name = "JFTF_CATEGORIAS", catalog = "", schema = "SCOTT")
@NamedQueries({
    //@NamedQuery(name = "Categoria.findAll", query = "SELECT c FROM Categoria c"),
    //@NamedQuery(name = "Categoria.findById", query = "SELECT c FROM Categoria c WHERE c.id = :id"),
    //@NamedQuery(name = "Categoria.findByCategoria", query = "SELECT c FROM Categoria c WHERE c.categoria = :categoria"),
    @NamedQuery(name = "Categoria.findByDescripcion", query = "SELECT c FROM Categoria c WHERE c.descripcion = :descripcion")
})
public class Categoria implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    private Long id;
    @Column(name = "CATEGORIA", length = 50)
    private String categoria;
    @Column(name = "DESCRIPCION", length = 250)
    private String descripcion;
    @OneToMany(mappedBy = "idCategoria", fetch = FetchType.EAGER)
    //@OneToMany(mappedBy = "idCategoria", fetch = FetchType.LAZY)
    private Collection<Producto> productoCollection;

    @Transient
    private List<Categoria> categoriaItems;
    public Categoria() {
    }

    public Categoria(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCategoria() {
        return categoria;
    }

    public void setCategoria(String categoria) {
        this.categoria = categoria;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public Collection<Producto> getProductoCollection() {
        return productoCollection;
    }

    public void setProductoCollection(Collection<Producto> productoCollection) {
        this.productoCollection = productoCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Categoria)) {
            return false;
        }
        Categoria other = (Categoria) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.cursoexperto.jftf.carrocompra.entidades.Categoria[id=" + id + "]";
    }

    /**
     * @return the categoriaItems
     */
    public List<Categoria> getCategoriaItems() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        CategoriaService jpaController = (CategoriaService) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "categoriaService");
        return jpaController.findCategoriaEntities();
    }

    /**
     * @param categoriaItems the categoriaItems to set
     */
    public void setCategoriaItems(List<Categoria> categoriaItems) {
        this.categoriaItems = categoriaItems;
    }

}
